package com.longge;

import java.util.HashMap;

public class CountCharacters {

    public int countCharacters(String[] words, String chars) {

        int ans = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : chars.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }


        for (String str : words) {
            char[] charArray = str.toCharArray();
            boolean flag = false;
            HashMap<Character, Integer> tempMap = new HashMap<>(map);
            for (char c : charArray) {
                if (tempMap.get(c) == null || tempMap.get(c) == 0) {
                    flag = true;
                    break;
                }
                tempMap.put(c, tempMap.getOrDefault(c, 0) + 1);
            }
            if (!flag) {
                ans += str.length();
            }
        }
        return ans;

    }

}
